use core::base_node::{NodeData, BaseNode, TBaseNode, NodeStatus};
use core::tick::Tick;
use core::behavior_tree::NodeTypeRegister;
use std::hash::Hash;
use std::collections::HashMap;

pub struct Composite {
    base_node: NodeData<BaseNode>,
    pub children: Vec<Box<TBaseNode>>
}

impl Composite {
    fn new(config: serde_json::Value)->Result<Composite, String> {
        match BaseNode::new(config) {
            Ok(_n) => {
                Ok(Composite {
                    base_node: NodeData::new(_n),
                    children: Vec::new()
                })
            }
            Err(_e) => {
                Err(_e)
            }
        }
    }
    pub fn register(container: &mut NodeTypeRegister, name: String) {
        container.register_type(name, Box::new(|config: serde_json::Value, nodes: &mut HashMap<String, Box<TBaseNode>>| {
            match Composite::new(config) {
                Ok(_n) => {
                    nodes.insert(_n.base_node.id.to_string(), Box::new(_n));
                }
                Err(_e) => {}
            };
        }));
    }
}
impl TBaseNode for Composite {
    fn add_child(&mut self, child: Box<TBaseNode>) {
        self.children.push(child)
    }
    fn get_base_node_ref(&self) -> NodeData<BaseNode> {
        self.base_node.clone()
    }
    fn execute(&self, tick: &mut Tick) -> NodeStatus {
        BaseNode::_execute(self.base_node.clone(), tick, self)
    }
}